﻿using LPP.Entity.AppUser;
using LPP.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace LPP.Service.UserRepository
{
    public interface IUserRepository
    {
        Task<Users> AddUserAsync(Users user);
        Task AddContactsAsync(List<Contact> contacts);
        Task<Users> GetUserByPhoneNumberAsync(string phoneNumbe);
    }

    public class UserRepository : IUserRepository
    {
        private readonly SqlDbContext _dbContext;

        public UserRepository(SqlDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public async Task<Users> AddUserAsync(Users user)
        {
            _dbContext.Users.Add(user);
            await _dbContext.SaveChangesAsync();
            return user;
        }

        public async Task AddContactsAsync(List<Contact> contacts)
        {
            _dbContext.Contacts.AddRange(contacts);
            await _dbContext.SaveChangesAsync();
        }
        public async Task<Users> GetUserByPhoneNumberAsync(string phoneNumber)
        {
            return await _dbContext.Users
                                 .FirstOrDefaultAsync(u => u.PhoneNumber == phoneNumber && !u.IsDelete); // 假设用户表有 IsDelete 字段
        }
    }
}
